/*
 * Copyright (C) 2014 VenomVendor <info@VenomVendor.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.bocai.dahuatech.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.dahuatech.Bean.ProductListBean;
import com.bocai.dahuatech.R;
import com.bocai.dahuatech.ui.activity.NewProductDetailActivity;
import com.bumptech.glide.Glide;

import java.util.List;

public class ProductGridAdapterHolder extends RecyclerView.Adapter<ProductGridAdapterHolder.ViewHolder> {

    OnItemClickListener mItemClickListener;
    private Context context;
    List<ProductListBean.ContentEntity> datas;
    public ProductGridAdapterHolder(Context context, List<ProductListBean.ContentEntity> datas){
        this.context=context;
        this.datas=datas;
    }
    public void addDatas(List<ProductListBean.ContentEntity> beans){
        datas.addAll(beans);
        this.notifyDataSetChanged();
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final LayoutInflater mInflater = LayoutInflater.from(parent.getContext());
        final View sView = mInflater.inflate(R.layout.product_third_grid_item, parent, false);
        return new ViewHolder(sView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.llView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, NewProductDetailActivity.class);
                intent.putExtra("productId", datas.get(position).getFID());
                intent.putExtra("solutionId", "0");
                context.startActivity(intent);
            }
        });
        holder.tvModel.setText(datas.get(position).getFOutModel());
        Glide.with(context).load(datas.get(position).getFPicPath())
                .into(holder.ivPic);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public void SetOnItemClickListener(final OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }


    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView  tvModel;
        ImageView ivPic;
        LinearLayout llView;
        public ViewHolder(View view) {
            super(view);
            llView=(LinearLayout)view.findViewById(R.id.ll_product_third_grid_view);
            tvModel=(TextView)view.findViewById(R.id.tv_product_third_grid_item_model);
            ivPic= (ImageView) view.findViewById(R.id.iv_product_third_grid_item_pic);
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mItemClickListener != null) {
//                mItemClickListener.onItemClick(v, getPosition());
                mItemClickListener.onItemClick(v, getLayoutPosition());
            }
        }

    }

}
